package com.spt.springbootzhsx.service.suggest.Get_MTMC_mul_UWL_5;

import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.Iterator;

/*
 **@Author:邓少勋
 **Created on 2022-06-10
 */
public class MU_Reducer extends Reducer<MUbean, Text, Text, DoubleWritable> {
    @Override
    protected void reduce(MUbean key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        Iterator<Text> iter = values.iterator();
        //拿到电影项：20005:2,20002:2,20001:3,20007:1,20006:2
        String[] movies = iter.next().toString().split(",");
        while (iter.hasNext()) {
            //拿到用户观影向量：10001:1,10004:1,10005:1
            String[] users = iter.next().toString().split(",");
//                System.out.println(Arrays.toString(users));
            for (String user : users) {
                String[] uid_nums = user.split(":");
                for (String movie : movies ) {
                    String[] mid_nums = movie.split(":");
                    //sb作为key输出
                    StringBuffer sb = new StringBuffer();
                    sb.append(uid_nums[0]).append(",").append(mid_nums[0]);
                    context.write(new Text(sb.toString()), new DoubleWritable(Double.parseDouble(uid_nums[1]) * Double.parseDouble(mid_nums[1])));
                    sb.setLength(0);
                }
            }
        }
    }
}
